package com.sphd.restaurantsystem.activitys.funding;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.Button;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.InvestRuleInfo;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.AddSharePresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.AddShareView;
import com.wzh.frame.utils.MyUtils;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增投资人
 */
public class AddShareholderActivity extends BaseActivity implements AddShareView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.m_name)
    MaterialEditText mName;
    @BindView(R.id.m_phone)
    MaterialEditText mPhone;
    @BindView(R.id.m_account_money)
    MaterialEditText mAccountMoney;
    @BindView(R.id.btn_over)
    Button btnOver;
    Intent intent;
    Bundle bundle;
    LoginInfo loginInfo;
    RestaurantInfo restaurantInfo;
   List<InvestRuleInfo> investRuleInfos;
    AddSharePresenterImpl addSharePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_shareholder);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        intent=getIntent();
        bundle=intent.getExtras();
        setTitleBar(toolbar,"新建投资人");
        addSharePresenter=new AddSharePresenterImpl(this);
        loginInfo= DataSupport.findLast(LoginInfo.class);
        restaurantInfo= (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        investRuleInfos= (List<InvestRuleInfo>) bundle.getSerializable(Constant.INVESTRULE);
        if (investRuleInfos.size()!=0)
        mAccountMoney.setHint("最低金额" + investRuleInfos.get(0).getAccount_money());
    }

    @OnClick(R.id.btn_over)
    public void onClick() {
        checkNull();
    }

    private void checkNull() {
       String name = mName.getText().toString().trim();
        String  phone = mPhone.getText().toString().trim();
        String accountMoney = mAccountMoney.getText().toString().trim();

        if (StringUtils.isEmpty(name)){
            toast("姓名不能为空");
        }else if (StringUtils.containsEmoji(name)){
            toast("请输入正确的姓名");
        }else if (TextUtils.isEmpty(phone) || !MyUtils.isMobileNO(phone)){
            toast("请输入正确的手机号码");
        }else if (TextUtils.isEmpty(accountMoney) || Double.parseDouble(accountMoney) <= 0 || investRuleInfos.get(0).getAccount_money()
                > Double.parseDouble(accountMoney)){
            toast("输入的金额不能低于最低投资金额");
        }else {
            addSharePresenter.addData(
            RestaurantUrl.Httptitle + RestaurantUrl.ADDFUNDING +
                    "cgid=" + restaurantInfo.getId() + "&name=" + StringUtils.utf8Encode(name) + "&phone=" + phone + "&money=" +
                    accountMoney + "&super_phone=" + loginInfo.getStaff_phone() + "&permillage=" + investRuleInfos.get(0).getAccount_permillage());
        }

    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("添加成功");
            setResult(1);
            finish();
        }else {
            toast("手机号码重复，请更换手机号码！");
            mPhone.setError("手机号码重复，请更换手机号码！");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
